#include "record.h"
#include "gtc_client_struct.h"
#include "fsapi.h"
#include "log.h"
#include <stdio.h>
#include <string.h>

static void record_add(char_t *name, char_t *str){
    fs_fd fp;
    int ret = fs_open(&fp, name, FS_O_CREAT|FS_O_WRONLY);
    if (ret == 0){
        fs_lseek(&fp, 0, FS_SEEK_END);
	    	fs_write(&fp, (char *)str, strlen(str));
        fs_sync(&fp);
		    fs_close(&fp);
    }
}

void record_update(char_t *dno, char_t *str){
    fs_fd fp;
    char_t dir[100]={0};
    
    sprintf(dir, "%s/%s", RECORD_CHARGING_DIR, dno);
    int32_t ret = fs_open(&fp, dir, FS_O_CREAT|FS_O_RDWR);
    if (ret != 0){
        log(LOG_ERR, "open file :%s fail\r\n", dir);
        return;
    }   
    //printf("record_update_str:%s\r\n", dir);
    fs_lseek(&fp, 0, FS_SEEK_SET);
    fs_write(&fp, str, strlen(str));
    fs_sync(&fp);
	  fs_close(&fp);    
}


int record_read_size(const char *name) {
    if(!fs_exist(name)) {
        return 0;
    }
    fs_fd fp;
    int ret = fs_open(&fp, name, FS_O_RDWR);
    if (ret != 0){
        log(LOG_ERR, "open file :%s fail\r\n", name);
        return 0;
    }
    
    int size = (int)(fs_size(&fp));
    fs_close(&fp);
    return size;
}

void record_file_rm(const char *name) {
    if (fs_exist(name)) {
        fs_delete(name);
    }
}

static void record_history_add(char_t *str){
    printf("record_history_add_str\r\n");
    int32_t file1size = record_read_size(RECORD_HISTORY1);
    if (file1size < RECORD_HISTORY_FILE_LEN_MAX){
        record_add(RECORD_HISTORY1, str);
        if (file1size + strlen(str) >= RECORD_HISTORY_MAX){
            record_file_rm(RECORD_HISTORY2);
        }
    }else{
        int32_t file2size = record_read_size(RECORD_HISTORY2);
        if (file2size < RECORD_HISTORY_FILE_LEN_MAX){
            record_add(RECORD_HISTORY2, str);
            if (file2size + strlen(str) >= RECORD_HISTORY_MAX){
                record_file_rm(RECORD_HISTORY1);
            }
        }else{
            record_file_rm(RECORD_HISTORY1);
            record_add(RECORD_HISTORY1, str);
        }
    }
}

static int32_t record_read_charging_with_dir(const char_t *dir, char_t *buf, int32_t len){
    fs_fd fp;
    int32_t ret = fs_open(&fp, dir, FS_O_RDWR);
    if (ret != 0){
        log(LOG_ERR, "open file :%s fail\r\n", dir);
        return -1;
    } 

    fs_lseek(&fp, 0, FS_SEEK_SET);
    fs_read(&fp, buf, len);
    fs_close(&fp);
    return 1;
}

int32_t record_read_charging_whth_dno(const char_t *dno, char_t *buf, int32_t len){
    char_t dir[100]={0};   
    sprintf(dir, "%s/%s", RECORD_CHARGING_DIR, dno);    
    return record_read_charging_with_dir(dir, buf, len);
}

int32_t record_read_charging_file_list(char_t *name_list, int32_t name_len, int32_t cnt){
    return fs_file_list(RECORD_CHARGING_DIR, name_list, name_len, cnt);
}

void record_save_to_history(const char *dno){
    char_t buf[1000]={0};
    char_t dir[100]={0};   
    sprintf(dir, "%s/%s", RECORD_CHARGING_DIR, dno);
    
    if(record_read_charging_with_dir(dir, buf, sizeof(buf)) > 0){
        record_history_add(buf);
        fs_delete(dir);
    }    
}

void record_init(void){
    fs_mkdir(RECORD_CHARGING_DIR);
}


